/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.crm.cs.web.TaskGroupAction
   Module Description   :

   Date Created      : 2011/2/14
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.crm.cs.web;

import java.util.Date;

import javax.annotation.Resource;

import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.crm.workorder.entity.TaskGroup;
import com.yaodian100.crm.workorder.entity.TaskGroupUser;
import com.yaodian100.crm.workorder.service.TaskGroupService;

/**
 * @author cut7man
 * 
 */
public class TaskGroupAction {

	private String iamlazy;
	private String id;
	private String name;
	private String parentId;
	private String enable;
	private TaskGroup taskGroup;
	private String[] manager;
	private String[] approver;
	private String[] observer;
	private String[] member;
	private String defaultGroup;
	private String groupUserId;
	private String taskRole;
	private String username;
	@Resource
	private BaseManager baseMgr;
	@Resource
	private TaskGroupService taskGroupService;
	private String message;

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the taskRole
	 */
	public String getTaskRole() {
		return taskRole;
	}

	/**
	 * @param taskRole the taskRole to set
	 */
	public void setTaskRole(String taskRole) {
		this.taskRole = taskRole;
	}

	/**
	 * @return the groupUserId
	 */
	public String getGroupUserId() {
		return groupUserId;
	}

	/**
	 * @param groupUserId the groupUserId to set
	 */
	public void setGroupUserId(String groupUserId) {
		this.groupUserId = groupUserId;
	}

	/**
	 * @return the defaultGroup
	 */
	public String getDefaultGroup() {
		return defaultGroup;
	}

	/**
	 * @param defaultGroup the defaultGroup to set
	 */
	public void setDefaultGroup(String defaultGroup) {
		this.defaultGroup = defaultGroup;
	}

	/**
	 * @return the manager
	 */
	public String[] getManager() {
		return manager;
	}

	/**
	 * @param manager the manager to set
	 */
	public void setManager(String[] manager) {
		this.manager = manager;
	}

	/**
	 * @return the approver
	 */
	public String[] getApprover() {
		return approver;
	}

	/**
	 * @param approver the approver to set
	 */
	public void setApprover(String[] approver) {
		this.approver = approver;
	}

	/**
	 * @return the observer
	 */
	public String[] getObserver() {
		return observer;
	}

	/**
	 * @param observer the observer to set
	 */
	public void setObserver(String[] observer) {
		this.observer = observer;
	}

	/**
	 * @return the member
	 */
	public String[] getMember() {
		return member;
	}

	/**
	 * @param member the member to set
	 */
	public void setMember(String[] member) {
		this.member = member;
	}

	/**
	 * @return the baseMgr
	 */
	public BaseManager getBaseMgr() {
		return baseMgr;
	}

	/**
	 * @param baseMgr the baseMgr to set
	 */
	public void setBaseMgr(BaseManager baseMgr) {
		this.baseMgr = baseMgr;
	}

	/**
	 * @return the taskGroup
	 */
	public TaskGroup getTaskGroup() {
		return taskGroup;
	}

	/**
	 * @param taskGroup the taskGroup to set
	 */
	public void setTaskGroup(TaskGroup taskGroup) {
		this.taskGroup = taskGroup;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the enable
	 */
	public String getEnable() {
		return enable;
	}

	/**
	 * @param enable the enable to set
	 */
	public void setEnable(String enable) {
		this.enable = enable;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the parentId
	 */
	public String getParentId() {
		return parentId;
	}

	/**
	 * @param parentId the parentId to set
	 */
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	/**
	 * @return the iamlazy
	 */
	public String getIamlazy() {
		return iamlazy;
	}

	/**
	 * @param iamlazy the iamlazy to set
	 */
	public void setIamlazy(String iamlazy) {
		this.iamlazy = iamlazy;
	}

	public String page() {

		return iamlazy;
	}

	public String update() {
		TaskGroup taskGroup = (TaskGroup) baseMgr.get(TaskGroup.class, id);
		boolean isSave = false;
		if (taskGroup == null) {
			taskGroup = new TaskGroup();
			taskGroup.setId(id);
			taskGroup.setCreateDate(new Date());
			isSave = true;
		}
		if (parentId != null) {
			TaskGroup parentGroup = (TaskGroup) baseMgr.get(TaskGroup.class, parentId);
			taskGroup.setParent(parentGroup);
		}
		//
		taskGroup.setName(name);
		taskGroup.setEnable(Boolean.valueOf(enable));
		if (isSave) {
			baseMgr.save(taskGroup);
			message = "已储存";
		} else {
			baseMgr.update(taskGroup);
			message = "已储存修改";
		}
		this.taskGroup = taskGroup;
		return "update";
	}

	public String enable() {
		TaskGroup taskGroup = (TaskGroup) baseMgr.get(TaskGroup.class, id);
		taskGroup.setEnable(true);
		return "enable";
	}

	public String disable() {
		TaskGroup taskGroup = (TaskGroup) baseMgr.get(TaskGroup.class, id);
		taskGroup.setEnable(false);
		return "disable";
	}

	public String updateGroupUser() {
		TaskGroup taskGroup = (TaskGroup) baseMgr.get(TaskGroup.class, id);
		String hql = "from TaskGroupUser where taskGroup.id=? and taskRole=?";
		Object[] managers = baseMgr.queryByHql(hql, new Object[] { id, TaskGroup.GROUP_ROLE.MANAGER.getValue() });
		Object[] approvers = baseMgr.queryByHql(hql, new Object[] { id, TaskGroup.GROUP_ROLE.APPROVER.getValue() });
		Object[] observers = baseMgr.queryByHql(hql, new Object[] { id, TaskGroup.GROUP_ROLE.OBSERVER.getValue() });
		Object[] members = baseMgr.queryByHql(hql, new Object[] { id, TaskGroup.GROUP_ROLE.MEMBER.getValue() });
		// 新增
		updateGroupUsers(taskGroup, manager, managers, TaskGroup.GROUP_ROLE.MANAGER.getValue());
		updateGroupUsers(taskGroup, approver, approvers, TaskGroup.GROUP_ROLE.APPROVER.getValue());
		updateGroupUsers(taskGroup, observer, observers, TaskGroup.GROUP_ROLE.OBSERVER.getValue());
		updateGroupUsers(taskGroup, member, members, TaskGroup.GROUP_ROLE.MEMBER.getValue());
		// 移除
		deleteGroupUser(manager, managers);
		deleteGroupUser(approver, approvers);
		deleteGroupUser(observer, observers);
		deleteGroupUser(member, members);

		message = "已更新资料";
		return "updateGroupUser";
	}

	private void updateGroupUsers(TaskGroup taskGroup, String[] theUsers, Object[] theGroupUsers, String taskRole) {
		if (theUsers != null && theUsers.length > 0) {
			for (String theManager : theUsers) {
				boolean isExist = false;
				if (theGroupUsers != null && theGroupUsers.length > 0) {
					for (Object obj : theGroupUsers) {
						TaskGroupUser taskGroupUser = (TaskGroupUser) obj;
						if (taskGroupUser.getUser().getUsername().equals(theManager)) {
							isExist = true;
						}
					}
				}
				if (!isExist) {
					Long count = baseMgr.countByHql("from TaskGroupUser where user.username=?", new Object[] { theManager });
					TaskGroupUser tgu = new TaskGroupUser();
					tgu.setTaskGroup(taskGroup);
					tgu.setTaskRole(taskRole);
					if (count == null || count == 0) {
						tgu.setDefaultGroup(true);
					}
					User user = (User) baseMgr.get(User.class, theManager);
					tgu.setUser(user);
					baseMgr.save(tgu);
				}
			}
		}
	}

	private void deleteGroupUser(String[] theUsers, Object[] theGroupUsers) {
		if (theGroupUsers != null && theGroupUsers.length > 0) {
			for (Object obj : theGroupUsers) {
				TaskGroupUser taskGroupUser = (TaskGroupUser) obj;
				boolean isExist = false;
				if (theUsers != null && theUsers.length > 0) {
					for (String theManager : theUsers) {
						if (taskGroupUser.getUser().getUsername().equals(theManager)) {
							isExist = true;
						}
					}
				}
				if (!isExist) {
					baseMgr.delete(taskGroupUser);
				}

			}
		}
	}

	public String updateUser() {
		TaskGroupUser tgu = (TaskGroupUser) baseMgr.get(TaskGroupUser.class, groupUserId);
		if (tgu == null) {
			tgu = new TaskGroupUser();
		}
		if (defaultGroup != null) {
			tgu.setDefaultGroup(Boolean.valueOf(defaultGroup));
		}
		TaskGroup taskGroup = (TaskGroup) baseMgr.get(TaskGroup.class, id);
		tgu.setTaskGroup(taskGroup);
		tgu.setTaskRole(taskRole);
		User user = (User) baseMgr.get(User.class, username);
		tgu.setUser(user);
		baseMgr.update(tgu);
		return "updateUser";
	}

	public String deleteGroupUser() {
		baseMgr.delete(baseMgr.load(TaskGroupUser.class, Long.valueOf(groupUserId)));
		return "deleteGroupUser";
	}

	public String setDefaultGroup() {
		taskGroupService.setDefaultGroup(username, groupUserId);

		return "setDefaultGroup";
	}
}
